 //
//  HomePageViewController.m
//  wangzhe
//
//  Created by 李 佳伟 on 2017/10/12.
//  Copyright © 2017年 李 佳伟. All rights reserved.
//

#import "HomePageViewController.h"
#import "JTCSM.h"
#import "ShowImageView.h"
#import "GongGaoView.h"
#import "TypeView.h"
#import "JiHuoMaView.h"
#import "HomePageModel.h"
#import "KuaiJiePayViewController.h"
#import "TrueNameViewController.h"
#import "PayJiHuoMaViewController.h"
#import "WebViewController.h"
#import "NewPersonViewController.h"
#import "ZhifuView.h"
#import "ShouKuanViewController.h"
#import "ShareViewController.h"
#import "GetInfoClass.h"
#import "HuanKuanPageViewController.h"
#import "NewTuoKeViewController.h"
#import "NewShouYiViewController.h"
#import "NewFukuanmaViewController.h"
#import "NewTuiGuangViewController.h"
#import "HWScanViewController.h"
#import "BaoXianViewController.h"
typedef enum {
    kechengwebType   = 0,
    xinyongkawebType = 1,
    jinduwebType     = 2,
    
}gotoVCtype;

@interface HomePageViewController ()<ShowImageViewDelegate,JiHuoMaViewDelegate,UIAlertViewDelegate,UINavigationControllerDelegate>
{
    UIScrollView *backScrollV;
    NSMutableArray *topdataArray;
    NSMutableArray *dataarray;
    
    CGFloat imageHeight;
    GongGaoView *gongView;
    NSInteger page;
    CGFloat KgongnengViewHeight;
}
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic,strong ) UILabel *addresslabel;
@property (nonatomic,strong) NSMutableArray *bannerArray;
@property (nonatomic,strong) NSMutableArray *noticeArray;
@property (nonatomic,strong)    ShowImageView *imageV;
@end

@implementation HomePageViewController
- (void) viewDidAppear:(BOOL)animated
{   //黑底白字
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.hidden = YES;
    _addresslabel = [UILabel initWithFrame:CGRectMake(20, 20, 100, 44) text:@"唐山" font:18 textAlignment:NSTextAlignmentLeft texcolor:RGBACOLOR(89, 89, 89, 1) backgroundColor:nil];
//    [self.navigationController.view addSubview:_addresslabel];
    
    [[GetInfoClass shareSingle] getinfo];
    [GetInfoClass shareSingle].myblock = ^(UserInfoModel *Model) {
        _addresslabel.text = Model.address;
    };
    
    backScrollV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, -20, KCONTROLLERWith, KCONTROLLERHeight-29)];
    backScrollV.backgroundColor = [UIColor whiteColor];
    backScrollV.userInteractionEnabled = YES;
    backScrollV.bounces = NO;
    backScrollV.showsVerticalScrollIndicator = NO;
    [self.view addSubview:backScrollV];
    [self layoutGongNengView];

    [self layoutGonggaoView];
    [self layoutTitleView];
    [self layoutTypeView];
    
    if ([Kis_code integerValue] !=1) {
        
        [self getJihuomaInfo];
    }
    
    
    
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    _addresslabel.hidden = YES;
     [self removeTimer];
}
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    _addresslabel.hidden = NO;
    self.navigationController.delegate = self;
    page =0;

    [df isRemoveTarbar:NO];
    self.navigationController.navigationBar.hidden = NO;

    if ([Kis_code integerValue] !=1) {
        [self getJihuomaInfo];
    }else{

//
//
//        }else if ([Kis_verified integerValue] ==2) {
//            //申请中
//            [df showAlertWithTitle:@"请耐心等待！" message:@"实名认证审核中" btn1:@"知道了" btn2:nil btn3:nil delegate:self tag:123];
//        }else {
            //已认证
            [self theRequest];
//        }
        
    }

   
}



- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag == KAlertVTag+123) {
        [self.delegate gotoLoginVC];
    }
}


#pragma 顶部功能栏

- (void)layoutGongNengView{
    KgongnengViewHeight =(KSCREENWidth ==320)?190:210;
    UIView *gongnengView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KCONTROLLERWith, KgongnengViewHeight)];
    gongnengView.backgroundColor = KZhuTiRGB;
    [backScrollV addSubview:gongnengView];
    CGFloat imagewidth = 183/3;
    CGFloat imageHeight = 72/3;
    UIImageView *titleIamgeV = [[UIImageView alloc]initWithFrame:CGRectMake((KCONTROLLERWith-imagewidth)/2, 58, imagewidth, imageHeight)];
    titleIamgeV.image = [UIImage imageNamed:@"titleimage.png"];
    [gongnengView addSubview:titleIamgeV];
    
    TypeView *view;
    NSArray *titleArray = @[@"扫一扫",@"付款码",@"扫码收款",@"附近商家"];
    NSArray *imageArray = @[@"saoyisao.png",@"fukuanma.png",@"saomashoukuan.png",@"funjinshangjia.png"];
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:2];
    for (int i = 0; i <titleArray.count; i++) {
        HomePageModel *model = [[HomePageModel alloc]init];
        model.titleStr = titleArray[i];
        model.imagename = imageArray[i];
        [array addObject:model];
    }
    CGFloat spaceofLeft = 15;
    CGFloat typeviewWidth = (KCONTROLLERWith-5*spaceofLeft)/4;
    for (int i = 0 ; i<array.count; i++) {
        
        view= [[TypeView alloc]initWithFrame:CGRectMake(spaceofLeft+(typeviewWidth+spaceofLeft)*i,114, typeviewWidth, typeviewWidth) TypeViewType:headerType];
        HomePageModel *model = array[i];
        [view setvalueWithModel:model];
        view.tag = 2000+i;
        view.userInteractionEnabled = YES;
        [df addTapGestureRecognizerWithTarget:self action:@selector(gotoVC:) andaddView:view];
        [backScrollV addSubview:view];
    }
    
}

#pragma  顶部循环图片
- (void)layoutTitleView{
    if (KSCREENWidth==375) {
        imageHeight = 100;
    }else if(KSCREENWidth==320){
        imageHeight = 80;
    }else{
        imageHeight = 110;
    }
    
    topdataArray = [NSMutableArray arrayWithObjects:@"a.jpg",@"a.jpg", nil];
    _imageV = [[ShowImageView alloc]initWithFrame:CGRectMake(0, KVIEW_Y(gongView)+15, KSCREENWidth, imageHeight)];
    _imageV.delegate = self;
    [backScrollV addSubview:_imageV];
    
    [df newlineViewWithHeightofTop:KVIEW_Y(_imageV)+15 superView:backScrollV spacing:0 color:RGBACOLOR(153,153,153, .2) lineHeight:10];
}
- (void)layoutGonggaoView{
    
    gongView = [[GongGaoView alloc]initWithFrame:CGRectMake(10, KgongnengViewHeight+10, KCONTROLLERWith-20, 60) title:@"" contetnt:@""];
    [backScrollV addSubview:gongView];
    
    [df addTapGestureRecognizerWithTarget:self action:@selector(gotonotice:) andaddView:gongView];
    
    [df newlineViewWithHeightofTop:KVIEW_Y(gongView)+10 superView:backScrollV spacing:0 color:RGBACOLOR(153,153,153, .2) lineHeight:10];

}

- (void)gotonotice:(UITapGestureRecognizer *)tap{
    NewShouYiViewController *vc = [[NewShouYiViewController alloc]init];
    vc.nameTitle = @"我的收益";
    [self.navigationController pushViewController:vc animated:YES];
}

#define mark  showImageViewDelegate 
- (void)touchImageView:(NSInteger)num{
    if (num >= self.bannerArray.count) {
        return;
    }
    HomePageModel *model = self.bannerArray[num];
    if (model.link == nil || [model.link isEqualToString:@""]) {
        return;
    }
    WebViewController *vc = [[WebViewController alloc]init];
    vc.titleName = model.title;
    vc.urlStr = model.link;
    [self.navigationController pushViewController:vc animated:YES];
}

- (NSMutableArray *)getHomePageModel{
    NSArray *titleArray = @[@"我的拓客",@"信用卡还款",@"窝保险",@"我要推广",@"信用贷",@"信用卡申请",@"窝商城",@"更多"];
     NSArray *imageArray = @[@"tuoke.png",@"xinyongkahuank.png",@"baoxian.png",@"tuiguang.png",@"xinyongdai.png",@"xinyongkshenqing.png",@"shangcheng.png",@"more.png"];
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:2];
    for (int i = 0; i <titleArray.count; i++) {
        HomePageModel *model = [[HomePageModel alloc]init];
        model.titleStr = titleArray[i];
        model.imagename = imageArray[i];
        [array addObject:model];
    }
    
    return array;
    
}

- (void)layoutTypeView{
    NSMutableArray *mydataarray = [self getHomePageModel];
    CGFloat spaceofLeft = 15;
    CGFloat typeViewwidth = (KCONTROLLERWith-5*spaceofLeft)/4;
    int x = 0;
    int y = 0;
    TypeView *view;
    for (int i = 0 ; i<mydataarray.count; i++) {
        y = i/4;
        x = i%4;
        view= [[TypeView alloc]initWithFrame:CGRectMake(spaceofLeft+(typeViewwidth+spaceofLeft)*x, KVIEW_Y(_imageV)+45+(view.frame.size.height+30)*y, typeViewwidth, typeViewwidth) TypeViewType:footerType];
        HomePageModel *model = mydataarray[i];
        [view setvalueWithModel:model];
        view.tag = 1000+i;
        view.userInteractionEnabled = YES;
        [df addTapGestureRecognizerWithTarget:self action:@selector(gotoVC:) andaddView:view];
        [backScrollV addSubview:view];
    }
    [backScrollV setContentSize:CGSizeMake(KCONTROLLERWith, KVIEW_Y(view))];
}
- (void)gotoVC:(UITapGestureRecognizer *)tap{
    NSInteger tagNum = [tap view].tag;
    
    switch (tagNum) {
        case 1000:
            [self gotoNewTuoKeViewController];
            break;
        case 1001:
            [self gotoHuanKuanPageViewController];
            break;
        case 1002:
            [self gotoBaoxianVC];
            break;
        case 1003:
            [self gotoTuiGuangViewController];
            break;
        case 1004:
            [self gotoShouKuanVC];
            break;
        case 1005:
            [self gotoWebVCwithtype:kechengwebType];
            break;
        case 1006:

            break;
        case 1007:

            break;
        case 2000:
            [self gotoHWScanViewController];
            break;
        case 2001:
            [self gotoNewFukuanmaViewController];
            break;
        case 2002:
            [self gotoShouKuanVC];
            break;
        case 2003:
            [self gotoShouKuanVC];
            break;
            
        default:
            break;
    }
}

- (void)gotoTuiGuangViewController{
    
    NewTuiGuangViewController *vc = [[NewTuiGuangViewController alloc]init];
    vc.nameTitle = @"推广模版";
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)gotoHuanKuanPageViewController{
    
    HuanKuanPageViewController *vc = [[HuanKuanPageViewController alloc]init];
    vc.nameTitle = @"信用卡还款";
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)gotoNewTuoKeViewController{
    NewTuoKeViewController *vc = [[NewTuoKeViewController alloc]init];
    vc.nameTitle = @"我的收益";
    [self.navigationController pushViewController:vc animated:YES];

}

- (void)gotoBaoxianVC{
    
     BaoXianViewController*vc = [[BaoXianViewController alloc]init];
    vc.nameTitle = @"保险";
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (void)gotoKuaiJieVC{
    
    KuaiJiePayViewController*vc = [[KuaiJiePayViewController alloc]init];
    vc.nameTitle= @"快捷支付";
    [self.navigationController pushViewController:vc animated:YES];
    
}

//跳转新人专区VC
- (void)gotoNewPersongVC{
    NewPersonViewController *vc = [[NewPersonViewController alloc]init];
    vc.nameTitle = @"新人专区";
    [self.navigationController pushViewController:vc animated:YES];
}


//跳转收款VC
- (void)gotoShouKuanVC{
    ShouKuanViewController *vc = [[ShouKuanViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

//跳转收款VC
- (void)gotoWebVCwithtype:(gotoVCtype)type{
    WebViewController *vc = [[WebViewController alloc]init];
    

    switch (type) {
        case kechengwebType:
            vc.titleName = @"信贷";
            vc.urlStr =@"http://real.izhongyin.com/wxportal/loans/loansList?org_id=016100000000";

            break;
        case xinyongkawebType:
            vc.titleName = @"申请信用卡";
            vc.urlStr =@"http://real.izhongyin.com/wxportal/creditCard/bankCards.do?org_id=016100000000";
            
            break;
        case jinduwebType:
            vc.titleName = @"信用卡办理进度查询";
            vc.urlStr =@"http://real.izhongyin.com/wxportal/creditCard/creditBanksQuery";
            
            break;
            
        default:
            break;
    }
    
        [self.navigationController pushViewController:vc animated:YES];
  
}

- (void)gotoNewFukuanmaViewController{
    NewFukuanmaViewController *vc = [[NewFukuanmaViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)gotoHWScanViewController{
    HWScanViewController *vc = [[HWScanViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)theRequest{
    [self.bannerArray removeAllObjects];
    self.bannerArray = nil;
    self.bannerArray = [NSMutableArray array];
    [self.noticeArray removeAllObjects];
    self.noticeArray = nil;
    self.noticeArray = [NSMutableArray array];
    LoadData *request = [[LoadData alloc]init];
    [request loadDataPostAsynchronousWithURL:KURLPOSTHeadr(@"Index/index") withDict:@{@"token":KTokenString} superView:nil isAddtag:NO Block:^(id result) {
        
         NSString *isreturn = [NSString stringWithFormat:@"%@",[result objectForKey:@"status"]];
        if ([isreturn isEqualToString:@"1"]) {
            
            NSDictionary *dict = KResultOfKey(@"data");
            NSArray *bannerArr = [dict objectForKey:@"banner"];
            NSArray *noticeArr = [dict objectForKey:@"notice"];
            for (NSDictionary *bannerdict in bannerArr) {
                HomePageModel *model = [HomePageModel whc_ModelWithJson:bannerdict];
                [self.bannerArray addObject:model];
            }
            [_imageV viewDidLoadWithImageArray:self.bannerArray];
            for (NSDictionary *noticedict in noticeArr) {
                HomePageModel *model = [HomePageModel whc_ModelWithJson:noticedict];
                [self.noticeArray addObject:model];
            }
            if (self.noticeArray.count>0) {
                HomePageModel *model = self.noticeArray[0];
                gongView.contentLabel.text = model.title;
                gongView.mymodel = model;
                [self addTimer];

            }
            
            
        }
    }];
}

- (void)getJihuomaInfo{
   
    LoadData *request = [[LoadData alloc]init];
    [request loadDataPostAsynchronousWithURL:KURLPOSTHeadr(@"OrderBarcode/index") withDict:@{@"token":KTokenString} superView:self.view isAddtag:NO Block:^(id result) {
      
        NSString *isreturn = [NSString stringWithFormat:@"%@",[result objectForKey:@"status"]];
        if ([isreturn isEqualToString:@"1"]) {
            NSDictionary *dict = KResultOfKey(@"data");
            [JiHuoMaView shareSingle].delegate = self;
            [[JiHuoMaView shareSingle] show];
            [JiHuoMaView shareSingle].payknow =[dict objectForKey:@"directions"];
            [JiHuoMaView shareSingle].money =[dict objectForKey:@"price"];
            [JiHuoMaView shareSingle].number =[dict objectForKey:@"barcode"];
        }
        
    }];

}



#pragma mark 定时器

- (void)nextImage
{
    if (page == self.noticeArray.count-1) {
        page = 0;
    }else
    {
        page++;
    }
    HomePageModel *model = self.noticeArray[page];
    gongView.contentLabel.text = model.title;
    gongView.mymodel = model;
    
}
- (void)addTimer{
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)removeTimer
{
    [self.timer invalidate];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
